/**
 * 
 */
package systole.view.crud.surgery;

import java.awt.Frame;
import java.util.Calendar;
import javax.swing.JDialog;
import systole.domain.clinicalInformation.Surgery;
import systole.exceptions.ExceptionDAO;
import systole.utils.ImageUtils;
import systole.view.crud.controller.ControllerEntityEdition;

/**
 * @author jmj
 *
 */
public class ControllerSurgeryEdition extends ControllerEntityEdition {

    /**
     */
    public ControllerSurgeryEdition() {
        super();
        this.entityName = "Cirugía";
        this.newDescription = "Nueva";
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        Surgery currentSurgery = (Surgery) this.curretnEntity;
        JDialogSurgery formToLoad = (JDialogSurgery) this.editForm;

        currentSurgery.setName(formToLoad.getjEdtName().getText());
        currentSurgery.setDescription(formToLoad.getjTxtDescription().getText());
        currentSurgery.setLastUpdate(Calendar.getInstance());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        JDialogSurgery formToLoad = (JDialogSurgery) this.editForm;
        Surgery currentSurgery = (Surgery) this.curretnEntity;
        formToLoad.getjEdtName().setText(currentSurgery.getName());
        formToLoad.getjTxtDescription().setText(currentSurgery.getDescription());

    }
    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */

    @Override
    protected String valid() {
        JDialogSurgery formToValid = (JDialogSurgery) this.editForm;
        Surgery surgery = (Surgery) this.curretnEntity;

        if ((formToValid.getjEdtName().getText() == null) || (formToValid.getjEdtName().getText().isEmpty())) {
            return "Debe ingresar el nombre de la cirugía";
        }

        try {
            if (this.facadeDB.getSportBroker().existSport(formToValid.getjEdtName().getText(), surgery.getId())) {
                return "Ya existe un deporte con el nombre ingresado";
            }

        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            return ex.getMessage();
        }

        return null;
    }

    @Override
    protected void save() throws ExceptionDAO {
        if (this.isEditing()) {
            this.facadeDB.getSurgeryBroker().update((Surgery) this.curretnEntity);
        } else {
            this.facadeDB.getSurgeryBroker().insert((Surgery) this.curretnEntity);
        }
    }

    @Override
    protected void loadIconOnForm() {
        this.editForm.setIconImage(ImageUtils.buildImage("resources/icons/medic/surgery16.png"));
    }

    @Override
    protected Object createEntity() throws ExceptionDAO {
        return new Surgery();
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogSurgery(parent, this);
    }
}
